#include <iostream>
#include <cstdio>
#include <cassert>
#include <pthread.h>
#include <unistd.h>

using namespace std;

int g_val = 0;

string fun()
{
    return "我是独立的方法";
}

// 传过来的参数是arg  thread one
void *thread_routine(void *args)
{
    const char *name = (const char *)args;
    while (true)
    {
        fun();
        cout << "我是新线程,我正在运行！name：" << name << " : " << fun() << " : " << g_val++ << " &g_val: " << &g_val << endl;
        sleep(1);
    }
}

int main()
{
    // typedef unsigned long int pthread_t;
    pthread_t tid;
    //       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    //                     void *(*start_routine) (void *), void *arg);
    // 创建新线程
    int n = pthread_create(&tid, nullptr, thread_routine, (void *)"thread one");
    assert(n == 0);
    (void)n;

    // 主线程
    while (true)
    {
        char tidbuffer[64];
        snprintf(tidbuffer, sizeof(tidbuffer), "0x%x", tid);
        cout << "我是主线程，我正在运行！我创建出来的tid: " << tidbuffer << " : " << g_val << " &g_val: " << &g_val << endl;
        sleep(1);
    }

    return 0;
}